package com.ruoyi.crm.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.crm.domain.AssignRecord;
import com.ruoyi.crm.domain.Business;
import com.ruoyi.crm.domain.Clue;
import com.ruoyi.crm.mapper.AssignRecordMapper;
import com.ruoyi.crm.mapper.BusinessMapper;
import com.ruoyi.crm.mapper.ClueMapper;
import com.ruoyi.crm.service.IAssignRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分配记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-05-31
 */
@Service
public class AssignRecordServiceImpl implements IAssignRecordService 
{
    @Resource
    private AssignRecordMapper assignRecordMapper;
    @Resource
    private ClueMapper clueMapper;
    @Resource
    private BusinessMapper businessMapper;

    /**
     * 查询分配记录
     * 
     * @param id 分配记录主键
     * @return 分配记录
     */
    @Override
    public AssignRecord selectAssignRecordById(Long id)
    {
        return assignRecordMapper.selectAssignRecordById(id);
    }

    /**
     * 查询分配记录列表
     * 
     * @param assignRecord 分配记录
     * @return 分配记录
     */
    @Override
    public List<AssignRecord> selectAssignRecordList(AssignRecord assignRecord)
    {
        return assignRecordMapper.selectAssignRecordList(assignRecord);
    }

    /**
     * 新增分配记录
     * 1、修改同id的记录的latest值为0
     * 2、修改线索/商机的状态为已分配
     * 3、插入记录
     * @param assignRecord 分配记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAssignRecord(AssignRecord assignRecord)
    {
        AssignRecord.Type type = AssignRecord.Type.valueOf(assignRecord.getType());
        // 修改同id记录的latest为0
        assignRecordMapper.updateLatestStatus(type.getValue(), assignRecord.getAssignId(), AssignRecord.LatestStatus.STALE.getValue());
        // 修改状态为已分配
        assignRecord.setType(type.getValue());
        if(type == AssignRecord.Type.BUSINESS) {
            Business business = businessMapper.selectBusinessById(assignRecord.getAssignId());
            business.setStatus(Clue.Status.ASSIGNED.getValue());
            business.setLastUpdateTime(DateUtils.getNowDate());
            business.setUpdateBy(SecurityUtils.getUsername());
            businessMapper.updateBusiness(business);
        } else if(type == AssignRecord.Type.CLUE) {
            Clue clue = clueMapper.selectClueById(assignRecord.getAssignId());
            clue.setStatus(Clue.Status.ASSIGNED.getValue());
            clue.setUpdateTime(DateUtils.getNowDate());
            clue.setUpdateBy(SecurityUtils.getUsername());
            clue.setFalseCount(0L);
            clueMapper.updateClue(clue);
        }
        // 添加分配记录
        assignRecord.setCreateTime(DateUtils.getNowDate());
        assignRecord.setCreateBy(SecurityUtils.getUsername());
        assignRecord.setLatest(AssignRecord.LatestStatus.LATEST.getValue());
        return assignRecordMapper.insertAssignRecord(assignRecord);
    }

    /**
     * 修改分配记录
     * 
     * @param assignRecord 分配记录
     * @return 结果
     */
    @Override
    public int updateAssignRecord(AssignRecord assignRecord)
    {
        return assignRecordMapper.updateAssignRecord(assignRecord);
    }

    /**
     * 批量删除分配记录
     * 
     * @param ids 需要删除的分配记录主键
     * @return 结果
     */
    @Override
    public int deleteAssignRecordByIds(Long[] ids)
    {
        return assignRecordMapper.deleteAssignRecordByIds(ids);
    }

    /**
     * 删除分配记录信息
     * 
     * @param id 分配记录主键
     * @return 结果
     */
    @Override
    public int deleteAssignRecordById(Long id)
    {
        return assignRecordMapper.deleteAssignRecordById(id);
    }

    @Transactional
    @Override
    public int insertAssignRecords(List<AssignRecord> assignRecords) {
        int sum = 0;
        for (AssignRecord assignRecord : assignRecords) {
            sum += this.insertAssignRecord(assignRecord);
        }
        return sum;
    }

    /**
     * 修改指定分配记录的最新状态
     * @param assingId
     * @param type
     * @param stale
     * @return
     */
    @Override
    public int updateAssignRecord(Long assingId, AssignRecord.Type type, AssignRecord.LatestStatus stale) {
        return assignRecordMapper.updateLatestStatus(type.getValue(), assingId, stale.getValue());
    }
}
